package com.mark.test;

import com.mark.beetl.entity.User;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.ext.DebugInterceptor;

import javax.sql.DataSource;

/**
 * 代码生成工具
 *
 * @author maliqiang
 * @version 1.0
 * @create 2017-03-27
 */
public class CodeGenerator {
    public static void main(String[] args) throws Exception {
//        DataSource dataSource = null;
//        ConnectionSource source = ConnectionSourceHelper.getSingle(dataSource);
        ConnectionSource source = ConnectionSourceHelper.getSimple("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/beetl?useUnicode=true&characterEncoding=UTF-8", "beetl", "root", "");
        DBStyle mysql = new MySqlStyle();
        // sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql");
        // 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的，
        UnderlinedNameConversion nc = new UnderlinedNameConversion();
        // 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况
        SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});
        //根据表结构生成实体对象
        sqlManager.genPojoCodeToConsole("user");
        //生成beetlsql模板
        sqlManager.genSQLTemplateToConsole("user");
        sqlManager.genBuiltInSqlToConsole(User.class);
    }

}
